import React, { Component } from 'react';
import {
    View,
    Text,
    FlatList,
    TouchableOpacity,
    Image,
} from 'react-native';

import moment from 'moment'

import {HomeMessage} from './HomeMessage'
//数据库
const Realm = require('realm');

let realm = new Realm({ schema: [HomeMessage]});


var iconsArray = [

    {icomName:'出差申请单－list',name:'出差申请单'},
    {icomName:'现金预支单－list',name:'出差申请单'},
    {icomName:'预支还款单－list',name:'出差申请单'},
    {icomName:'出差费用报销单－list',name:'出差申请单'},
    {icomName:'日常费用报销单－nav',name:'出差申请单'},
    {icomName:'日常开支申请单－list',name:'出差申请单'},
    {icomName:'请假申请',name:'出差申请单'},
    {icomName:'加班申请',name:'出差申请单'},
    {icomName:'公告',name:'出差申请单'},
    {icomName:'日程_1',name:'出差申请单'},
    {icomName:'日报b',name:'出差申请单'},
    {icomName:'周报b',name:'出差申请单'},
    {icomName:'月报b',name:'出差申请单'},
];

var obj;
class MessageList extends Component {

    constructor(props) {
        super(props);
        this.state = {
            dataSourse: [],
        }
    }

    static navigationOptions = ({ navigation, screenProps }) => ({
        // title: navigation.state.params?navigation.state.params.title:null,
        headerTitle: navigation.state.params.title,
        headerStyle: { backgroundColor: 'rgba(0,171,236,1)' },//导航栏的样式

    });

    componentDidMount() {

        { this.requestListData() }
    }

    navigatePress = () => {
        this.props.navigation.goBack();
    }

    requestListData() {

      var sql = 'type == ' + this.props.navigation.state.params.rowData.item.id; 

      let HomeMessage = realm.objects('HomeMessage');
      // 设置筛选条件
      let data = HomeMessage.filtered(sql);
        var title = ''
        if(this.props.navigation.state.params.rowData.item.id == 0){
             title = '待办(' + data.length + ')'
        }else if(this.props.navigation.state.params.rowData.item.id == 1){
            title = '提醒'
        }else{
            title = '预警'
        }
        this.props.navigation.setParams({
            title:title,
            navigatePress: this.navigatePress,
        })
        this.setState({
            dataSourse:data,
        })
    }

    render() {
        obj = this;
        return (
            <View style={{ flex: 1 }}>

                <FlatList
                    keyExtractor={this._keyExtractor}
                    data={this.state.dataSourse}
                    renderItem={this._renderItem}
                />
            </View>
        );
    }

    _keyExtractor = (item, index) => {

        return (index + item.uuid)
    }

    _renderItem(rowData) {

        console.log(rowData)
        return (<TouchableOpacity style={{
            padding: 15, flexDirection: 'row', alignItems: 'center',
            borderBottomColor: '#e8e8e8', borderBottomWidth: 0.5, backgroundColor: 'white'
        }}
            onPress={() => obj.itemClick(rowData)}
        >

            <View style={{ marginRight: 20 }}>
                <Image source={{ uri: iconsArray[rowData.item.category].icomName}} style={{ width: 40, height: 40 }}>
                </Image>
            </View>
            <View style={{ flex: 1, height: 40, justifyContent: 'space-between' }}>
                <Text style={{ fontSize: 18 }}>{moment(rowData.item.sendDate).format("YYYY-MM-DD HH:mm")}</Text>
                <Text style={{ color: 'gray' }}>{rowData.item.content}</Text>
            </View>

        </TouchableOpacity>
        )
    }


    itemClick = (rowData) => {


    }
}

export default MessageList;
